Imports System.Data.SqlClient
Public Class Localidad : Implements IBusquedaFiltrado

    Private idLoca As Integer
    Private descri As String
    Private prov As Provincia


    Public Sub New()
        idLoca = 0
        descri = ""
        prov = Nothing
    End Sub

    Public Property Identificador() As Integer
        Get
            Return idLoca
        End Get
        Set(ByVal value As Integer)
            idLoca = value
        End Set
    End Property

    Public Property Descripcion() As String
        Get
            Return descri
        End Get
        Set(ByVal value As String)
            descri = value
        End Set
    End Property

    Public Property Provincia() As Provincia
        Get
            Return prov
        End Get
        Set(ByVal value As Provincia)
            prov = value
        End Set
    End Property

    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim comando As SqlCommand = New SqlCommand()
        Dim sql As String = ""

        sql = String.Format("INSERT INTO LOCALIDAD(IDLOC,DESCRIP,IDPROV) VALUES({0},'{1}',{2})", _
                            Me.Identificador, Me.descri, Me.prov.Identificador)

        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlCommand = New SqlCommand()
        Dim sql As String = "SELECT * FROM LOCALIDAD"
        Dim rsLocalidad As SqlDataReader
        Dim lResp As Boolean = False

        comando.Connection = con.ConexionBase
        comando.CommandText = sql

        rsLocalidad = comando.ExecuteReader()
        If rsLocalidad.Read() Then
            Me.Identificador = System.Convert.ToInt16(rsLocalidad("idLoc"))
            Me.descri = rsLocalidad("descrip").ToString
            'prov = New Provincia()
            'prov.Buscar(rsLocalidad("idProv").ToString, con)
            lResp = True
        Else
            lResp = False
        End If
        rsLocalidad.Close()
        Return lResp
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Eliminar
        Dim comando As SqlCommand = New SqlCommand()
        Dim sql As String = ""

        sql = String.Format("DELETE FROM LOCALIDAD WHERE IDLOC ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)

        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim comando As SqlCommand = New SqlCommand()
        Dim sql As String = ""

        sql = String.Format("UPDATE LOCALIDAD SET DESCRIP='{0}', IDPROV={1} WHERE IDLOC={2}", _
                            Me.descri, Me.prov.Identificador, Me.Identificador)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT * FROM LOCALIDAD"
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "localidad")

        Return datos
    End Function
    Public Function BuscarId(ByVal DescLoc As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM LOCALIDAD WHERE Descrip='{0}'", DescLoc)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            idLoca = System.Convert.ToInt16(rs("idLoc"))
            descri = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
End Class
